<?php

class Raid_ManageController extends Pldkp_Controller_Action
{
    
    public function indexAction()
    {
        $raidTable = new Raid_Model_Raids();
        $this->view->raids = $raidTable->fetchAll('start_time > NOW()', 'start_time DESC');
    }
    
    public function editAction()
    {
        $raid = Raid_Model_Raids::getById($this->_request->getParam('raid'));
        
        if (!$raid) {
            $this->setFlashMessage('Invalid raid!');
            $this->redirect(array('action' => 'index'));
        } 
        
        $form = new Raid_Form_RaidForm($raid);
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $dungeons = array();
                foreach ($form->getValue('dungeon_ids') as $dungeonId) {
                    $dungeon = Raid_Model_Dungeons::getById($dungeonId);
                    if ($dungeon) {
                        $dungeons[] = $dungeon;
                    }                    
                }
                
                $raid->raid_leader_user_id = $form->getValue('raid_leader_user_id');
                $raid->start_time = $form->getValue('start_time');
                $raid->end_time = $form->getValue('end_time');
                $raid->signup_deadline = $form->getValue('signup_deadline');
                $raid->note = $form->getValue('note');
                
                if ($raid->save()) {
                    $raid->setDungeons($dungeons);
                    $this->setFlashMessage('Raid edited!');
                    $this->redirect(array('action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not save the raid, please check that the dates are proper!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        } else {
            
            $dungeonIds = array();
          
            foreach ($raid->findDependentRowset('Raid_Model_RaidDungeons') as $raidDungeon) {
                $dungeonIds[] = $raidDungeon->dungeon_id;
            }
            
            $form->populate(array('raid_leader_user_id' => $raid->raid_leader_user_id, 
                                  'dungeon_ids' => $dungeonIds,
                                  'start_time' => $raid->start_time,
                                  'end_time' => $raid->end_time,
                                  'signup_deadline' => $raid->signup_deadline,
                                  'note' =>  $raid->note));
        }
        
        $this->view->raid = $raid;
        $this->view->form = $form;
    }
    
    public function createAction()
    {
        $form = new Raid_Form_RaidForm();
        
        if ($this->_request->isPost()) {
            $postData = $this->_request->getPost();
            if ($form->isValid($postData)) {
                
                $dungeons = array();
                foreach ($form->getValue('dungeon_ids') as $dungeonId) {
                    $dungeon = Raid_Model_Dungeons::getById($dungeonId);
                    if ($dungeon) {
                        $dungeons[] = $dungeon;
                    }                    
                }
                
                $raid = Raid_Model_Raids::getNew();
                
                $raid->raid_leader_user_id = $form->getValue('raid_leader_user_id');
                $raid->start_time = $form->getValue('start_time');
                $raid->end_time = $form->getValue('end_time');
                $raid->signup_deadline = $form->getValue('signup_deadline');
                $raid->note = $form->getValue('note');
                
                if ($raid->save()) {
                    $raid->setDungeons($dungeons);
                    $this->setFlashMessage('Raid created!');
                    $this->redirect(array('controller' => 'manage', 'action' => 'index'));
                } else {
                    $this->setInstantFlashMessage('Could not create raid!');
                    $form->populate($postData);
                }
            } else {
                $form->populate($postData);
            }
        } else {
            // This handles setting up the date if no other post parameters are set
            // Any date that can be parsed by Zend_Date will work
            $defaultDate = new Zend_Date();
            if ($this->_request->getParam('date')) {
                $defaultDate = new Zend_Date(urldecode($this->_request->getParam('date')));
            }
        
            $form->setDefaultDate($defaultDate);
        }
        
        $this->view->form = $form;
    }
}